//
//  TitleButton.m
//  Weibo
//
//  Created by Anasue on 9/10/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "TitleButton.h"

@implementation TitleButton

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentRight;
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.imageView.contentMode = UIViewContentModeCenter;
        self.titleLabel.font = NavigationTitleFont;
        //高亮状态下不调整内部图片为灰色
        self.adjustsImageWhenHighlighted = NO;
    
    }
    return self;

}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageY = 0;
    CGFloat imageW = self.height;
    CGFloat imageH = imageW;
    CGFloat imageX = self.width - imageW;
    return CGRectMake(imageX, imageY, imageW, imageH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = self.width -self.height;
    CGFloat titleH = self.height;
    return CGRectMake(titleX, titleY, titleW, titleH);

}

- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    CGSize titleSize = [title sizeWithFont:self.titleLabel.font];
    self.width = titleSize.width + self.height + 20;
}


@end
